home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / perl-5.003-complete / bin / perlbug < prev    next >
Text File  |  1996-08-15  |  14KB  |  611 lines

  1. #!/ade/bin/perl5
  2.     eval 'exec perl -S $0 "$@"'
  3.     if 0;
  4.  
  5. use Config;
  6. use Getopt::Std;
  7.  
  8. BEGIN {
  9.     eval "use Mail::Send;";
  10.     $::HaveSend = ($@ eq "");
  11.     eval "use Mail::Util;";
  12.     $::HaveUtil = ($@ eq "");
  13. };
  14.  
  15.  
  16. use strict;
  17.  
  18. sub paraprint;
  19.  
  20.  
  21. my($Version) = "1.13";
  22.  
  23. # Changed in 1.06 to skip Mail::Send and Mail::Util if not available.
  24. # Changed in 1.07 to see more sendmail execs, and added pipe output.
  25. # Changed in 1.08 to use correct address for sendmail.
  26. # Changed in 1.09 to close the REP file before calling it up in the editor.
  27. #                 Also removed some old comments duplicated elsewhere.
  28. # Changed in 1.10 to run under VMS without Mail::Send; also fixed
  29. #                 temp filename generation.
  30. # Changed in 1.11 to clean up some text and removed Mail::Send deactivator.
  31. # Changed in 1.12 to check for editor errors, make save/send distinction
  32. #                 clearer and add $ENV{REPLYTO}.
  33. # Changed in 1.13 to hopefully make it more difficult to accidentally
  34. #                 send mail
  35.  
  36. # TODO: Allow the user to re-name the file on mail failure, and
  37. #       make sure failure (transmission-wise) of Mail::Send is 
  38. #       accounted for.
  39.  
  40. my( $file, $cc, $address, $perlbug, $testaddress, $filename,
  41.     $subject, $from, $verbose, $ed, 
  42.     $fh, $me, $Is_VMS, $msg, $body, $andcc );
  43.  
  44. Init();
  45.  
  46. if($::opt_h) { Help(); exit; }
  47.  
  48. if(!-t STDIN) {
  49.     paraprint <<EOF;
  50. Please use perlbug interactively. If you want to 
  51. include a file, you can use the -f switch.
  52. EOF
  53.     die "\n";
  54. }
  55.  
  56. if($::opt_d or !-t STDOUT) { Dump(*STDOUT); exit; }
  57.  
  58. Query();
  59. Edit();
  60. NowWhat();
  61. Send();
  62.  
  63. exit;
  64.  
  65. sub Init {
  66.  
  67.     # -------- Setup --------
  68.  
  69.     $Is_VMS = $^O eq 'VMS';
  70.  
  71.     getopts("dhva:s:b:f:r:e:SCc:t");
  72.     
  73.  
  74.     # This comment is needed to notify metaconfig that we are
  75.     # using the $perladmin, $cf_by, and $cf_time definitions.
  76.  
  77.  
  78.     # -------- Configuration ---------
  79.     
  80.     # perlbug address
  81.     $perlbug = 'perlbug@perl.com';
  82.     
  83.     # Test address
  84.     $testaddress = 'perlbug-test@perl.com';
  85.     
  86.     # Target address
  87.     $address = $::opt_a || ($::opt_t ? $testaddress : $perlbug);
  88.  
  89.     # Possible administrator addresses, in order of confidence
  90.     # (Note that cf_email is not mentioned to metaconfig, since
  91.     # we don't really want it. We'll just take it if we have to.)
  92.     $cc = ($::opt_C ? "" : (
  93.         $::opt_c || $::Config{perladmin} || $::Config{cf_email} || $::Config{cf_by}
  94.         ));
  95.     
  96.     # Users address, used in message and in Reply-To header
  97.     $from = $::opt_r || "";
  98.  
  99.     # Include verbose configuration information
  100.     $verbose = $::opt_v || 0;
  101.  
  102.     # Subject of bug-report message
  103.     $subject = $::opt_s || "";
  104.  
  105.     # File to send as report
  106.     $file = $::opt_f || "";
  107.  
  108.     # Body of report
  109.     $body = $::opt_b || "";
  110.  
  111.     # Editor
  112.     $ed = ($::opt_f ? "file" : (
  113.             $::opt_e || $ENV{VISUAL} || $ENV{EDITOR} || $ENV{EDIT} || 
  114.               ($Is_VMS ? "edit/tpu" : "vi")
  115.           ));
  116.       
  117.     # My username
  118.     $me = getpwuid($<);
  119.  
  120. }
  121.  
  122.  
  123. sub Query {
  124.  
  125.     # Explain what perlbug is
  126.     
  127.     paraprint <<EOF;
  128. This program allows you to create a bug report,
  129. which will be sent as an e-mail message to $address
  130. once you have filled in the report.
  131.  
  132. EOF
  133.  
  134.  
  135.     # Prompt for subject of message, if needed
  136.     if(! $subject) {
  137.         paraprint <<EOF;
  138. First of all, please provide a subject for the 
  139. message. It should be as a concise description of 
  140. the bug as is possible.
  141.  
  142. EOF
  143.         print "Subject: ";
  144.     
  145.         $subject = <>;
  146.         chop $subject;
  147.     
  148.         my($err)=0;
  149.         while( $subject =~ /^\s*$/ ) {
  150.             print "\nPlease enter a subject: ";
  151.             $subject = <>;
  152.             chop $subject;
  153.             if($err++>5) {
  154.                 die "Aborting.\n";
  155.             }
  156.         }
  157.     }
  158.     
  159.  
  160.     # Prompt for return address, if needed
  161.     if( !$from) {
  162.  
  163.         # Try and guess return address
  164.         my($domain);
  165.         
  166.         if($::HaveUtil) {
  167.             $domain = Mail::Util::maildomain();
  168.         } elsif ($Is_VMS) {
  169.             require Sys::Hostname;
  170.             $domain = Sys::Hostname::hostname();
  171.         } else {
  172.             $domain = `hostname`.".".`domainname`;
  173.             $domain =~ s/[\r\n]+//g;
  174.         }
  175.         
  176.         my($guess);
  177.                          
  178.             if( !$domain) {
  179.                 $guess = "";
  180.             } elsif ($Is_VMS && !$::Config{'d_has_sockets'}) { 
  181.                 $guess = "$domain\:\:$me";
  182.             } else {
  183.                 $guess = "$me\@$domain" if $domain;
  184.                 $guess = "$me\@unknown.addresss" unless $domain;
  185.             }
  186.             
  187.         $guess = $ENV{'REPLYTO'} if defined($ENV{'REPLYTO'});
  188.         $guess = $ENV{"REPLY-TO"} if defined($ENV{'REPLY-TO'});
  189.     
  190.         if( $guess ) {
  191.             paraprint <<EOF;
  192.  
  193.  
  194. Your e-mail address will be useful if you need to be contacted. If the
  195. default shown is not your full internet e-mail address, please correct it.
  196.  
  197. EOF
  198.         } else {
  199.             paraprint <<EOF;
  200.  
  201. So that you may be contacted if necessary, please enter 
  202. your full internet e-mail address here.
  203.  
  204. EOF
  205.         }
  206.         print "Your address [$guess]: ";
  207.     
  208.         $from = <>;
  209.         chop $from;
  210.     
  211.         if($from eq "") { $from = $guess }
  212.     
  213.     }
  214.     
  215.     #if( $from =~ /^(.*)\@(.*)$/ ) {
  216.     #    $mailname = $1;
  217.     #    $maildomain = $2;
  218.     #}
  219.  
  220.     if( $from eq $cc or $me eq $cc ) {
  221.         # Try not to copy ourselves
  222.         $cc = "yourself";
  223.     }
  224.  
  225.  
  226.     # Prompt for administrator address, unless an override was given
  227.     if( !$::opt_C and !$::opt_c ) {
  228.         paraprint <<EOF;
  229.  
  230.  
  231. A copy of this report can be sent to your local
  232. perl administrator. If the address is wrong, please 
  233. correct it, or enter 'none' or 'yourself' to not send
  234. a copy.
  235.  
  236. EOF
  237.  
  238.         print "Local perl administrator [$cc]: ";
  239.     
  240.         my($entry) = scalar(<>);
  241.         chop $entry;
  242.     
  243.         if($entry ne "") {
  244.             $cc = $entry;
  245.             if($me eq $cc) { $cc = "" }
  246.         }
  247.     
  248.     }
  249.  
  250.     if($cc =~ /^(none|yourself|me|myself|ourselves)$/i) { $cc = "" }
  251.  
  252.     $andcc = " and $cc" if $cc;
  253.  
  254.  
  255.     # Prompt for editor, if no override is given
  256.     if(! $::opt_e and ! $::opt_f and ! $::opt_b) {
  257.         paraprint <<EOF;
  258.  
  259.  
  260. Now you need to supply the bug report. Try to make
  261. the report concise but descriptive. Include any 
  262. relevant detail. Some information about your local
  263. perl configuration will automatically be included 
  264. at the end of the report. 
  265.  
  266. You will probably want to use an editor to enter
  267. the report. If "$ed" is the editor you want
  268. to use, then just press Enter, otherwise type in
  269. the name of the editor you would like to use.
  270.  
  271. If you would like to use a prepared file, type
  272. "file", and you will be asked for the filename.
  273.  
  274. EOF
  275.  
  276.         print "Editor [$ed]: ";
  277.     
  278.         my($entry) =scalar(<>);
  279.         chop $entry;
  280.     
  281.         if($entry ne "") {
  282.             $ed = $entry;
  283.         } 
  284.     }
  285.  
  286.  
  287.     # Generate scratch file to edit report in
  288.     
  289.     {
  290.     my($dir) = $Is_VMS ? 'sys$scratch:' : '/tmp/';
  291.     $filename = "bugrep0$$";
  292.     $filename++ while -e "$dir$filename";
  293.     $filename = "$dir$filename";
  294.     }
  295.     
  296.     
  297.     # Prompt for file to read report from, if needed
  298.     
  299.     if( $ed eq "file" and ! $file) {
  300.         paraprint <<EOF;
  301.  
  302.  
  303. What is the name of the file that contains your report?
  304.  
  305. EOF
  306.  
  307.         print "Filename: ";
  308.     
  309.         my($entry) = scalar(<>);
  310.         chop($entry);
  311.  
  312.         if(!-f $entry or !-r $entry) {
  313.             print "\n\nUnable to read from `$entry'.\nExiting.\n";
  314.             exit;
  315.         }
  316.         $file = $entry;
  317.  
  318.     }
  319.  
  320.  
  321.     # Generate report
  322.  
  323.     open(REP,">$filename");
  324.  
  325.     print REP <<EOF;
  326. This is a bug report for perl from $from,
  327. generated with the help of perlbug $Version running under perl $].
  328.  
  329. EOF
  330.  
  331.     if($body) {
  332.         print REP $body;
  333.     } elsif($file) {
  334.         open(F,"<$file") or die "Unable to read report file: $!\n";
  335.         while(<F>) {
  336.         print REP $_
  337.         }
  338.         close(F);
  339.     } else {
  340.         print REP "[Please enter your report here]\n";
  341.     }
  342.     
  343.     Dump(*REP);
  344.     close(REP);
  345.  
  346. }
  347.  
  348. sub Dump {
  349.     local(*OUT) = @_;
  350.     
  351.     print OUT <<EOF;
  352.  
  353.  
  354.  
  355. Site configuration information for perl $]:
  356.  
  357. EOF
  358.  
  359.     if( $::Config{cf_by} and $::Config{cf_time}) {
  360.         print OUT "Configured by $::Config{cf_by} at $::Config{cf_time}.\n\n";
  361.     }
  362.  
  363.     print OUT Config::myconfig;
  364.  
  365.     if($verbose) {
  366.         print OUT "\nComplete configuration data for perl $]:\n\n";
  367.         my($value);
  368.         foreach (sort keys %::Config) {
  369.             $value = $::Config{$_};
  370.             $value =~ s/'/\\'/g;
  371.             print OUT "$_='$value'\n";
  372.         }
  373.     }
  374. }
  375.  
  376. sub Edit {
  377.     # Edit the report
  378.     
  379. tryagain:    
  380.     if(!$file and !$body) {
  381.         my($sts) = system("$ed $filename");
  382.         if( $Is_VMS ? !($sts & 1) : $sts ) {
  383.             #print "\nUnable to run editor!\n";
  384.             paraprint <<EOF;
  385.  
  386. The editor you chose (`$ed') could apparently not be run!
  387. Did you mistype the name of your editor? If so, please
  388. correct it here, otherwise just press Enter. 
  389.  
  390. EOF
  391.             print "Editor [$ed]: ";
  392.         
  393.             my($entry) =scalar(<>);
  394.             chop $entry;
  395.     
  396.             if($entry ne "") {
  397.                 $ed = $entry;
  398.                 goto tryagain;
  399.             } else {
  400.             
  401.             paraprint <<EOF;
  402.  
  403. You may want to save your report to a file, so you can edit and mail it
  404. yourself.
  405. EOF
  406.             }
  407.         } 
  408.     }
  409. }
  410.  
  411. sub NowWhat {
  412.  
  413.     # Report is done, prompt for further action
  414.     if( !$::opt_S ) {
  415.         while(1) {
  416.  
  417.             paraprint <<EOF;
  418.  
  419.  
  420. Now that you have completed your report, would you like to send 
  421. the message to $address$andcc, display the message on 
  422. the screen, re-edit it, or cancel without sending anything?
  423. You may also save the message as a file to mail at another time.
  424.  
  425. EOF
  426.  
  427.             print "Action (Send/Display/Edit/Cancel/Save to File): ";
  428.             my($action) = scalar(<>);
  429.             chop $action;
  430.  
  431.             if( $action =~ /^(f|sa)/i ) { # <F>ile/<Sa>ve
  432.                 print "\n\nName of file to save message in [perlbug.rep]: ";
  433.                 my($file) = scalar(<>);
  434.                 chop $file;
  435.                 if($file eq "") { $file = "perlbug.rep" }
  436.             
  437.                 open(FILE,">$file");
  438.                 open(REP,"<$filename");
  439.                 print FILE "To: $address\nSubject: $subject\n";
  440.                 print FILE "Cc: $cc\n" if $cc;
  441.                 print FILE "Reply-To: $from\n" if $from;
  442.                 print FILE "\n";
  443.                 while(<REP>) { print FILE }
  444.                 close(REP);
  445.                 close(FILE);
  446.     
  447.                 print "\nMessage saved in `$file'.\n";
  448.                 exit;
  449.  
  450.             } elsif( $action =~ /^(d|l|sh)/i ) { # <D>isplay, <L>ist, <Sh>ow
  451.                 # Display the message
  452.                 open(REP,"<$filename");
  453.                 while(<REP>) { print $_ }
  454.                 close(REP);
  455.             } elsif( $action =~ /^se/i ) { # <S>end
  456.                 # Send the message
  457.                 print "\
  458. Are you certain you want to send this message?
  459. Please type \"yes\" if you are: ";
  460.                 my($reply) = scalar(<STDIN>);
  461.                 chop($reply);
  462.                 if( $reply eq "yes" ) {
  463.                     last;
  464.                 }
  465.             } elsif( $action =~ /^[er]/i ) { # <E>dit, <R>e-edit
  466.                 # edit the message
  467.                 Edit();
  468.                 #system("$ed $filename");
  469.             } elsif( $action =~ /^[qc]/i ) { # <C>ancel, <Q>uit
  470.                 1 while unlink($filename);  # remove all versions under VMS
  471.                 print "\nCancelling.\n";
  472.                 exit(0);
  473.             } elsif( $action =~ /^s/ ) {
  474.                 paraprint <<EOF;
  475.  
  476. I'm sorry, but I didn't understand that. Please type "send" or "save".
  477. EOF
  478.             }
  479.         
  480.         }
  481.     }
  482. }
  483.  
  484.  
  485. sub Send {
  486.  
  487.     # Message has been accepted for transmission -- Send the message
  488.     
  489.     if($::HaveSend) {
  490.  
  491.         $msg = new Mail::Send Subject => $subject, To => $address;
  492.     
  493.         $msg->cc($cc) if $cc;
  494.         $msg->add("Reply-To",$from) if $from;
  495.         
  496.         $fh = $msg->open;
  497.  
  498.         open(REP,"<$filename");
  499.         while(<REP>) { print $fh $_ }
  500.         close(REP);
  501.     
  502.         $fh->close;  
  503.     
  504.     } else {
  505.         if ($Is_VMS) {
  506.             if ( ($address =~ /@/ and $address !~ /^\w+%"/) or
  507.                  ($cc      =~ /@/ and $cc      !~ /^\w+%"/) ){
  508.                 my($prefix);
  509.                 foreach (qw[ IN MX SMTP UCX PONY WINS ],'') {
  510.                     $prefix = "$_%",last if $ENV{"MAIL\$PROTOCOL_$_"};
  511.                 }
  512.                 $address = qq[${prefix}"$address"] unless $address =~ /^\w+%"/;
  513.                 $cc = qq[${prefix}"$cc"] unless !$cc || $cc =~ /^\w+%"/;
  514.             }
  515.             $subject =~ s/"/""/g; $address =~ s/"/""/g; $cc =~ s/"/""/g;
  516.             my($sts) = system(qq[mail/Subject="$subject" $filename. "$address","$cc"]);
  517.             if (!($sts & 1)) { die "Can't spawn off mail\n\t(leaving bug report in $filename): $sts\n;" }
  518.         } else {
  519.             my($sendmail) = "";
  520.             
  521.             foreach (qw(/usr/lib/sendmail /usr/sbin/sendmail /usr/ucblib/sendmail))
  522.             {
  523.                 $sendmail = $_, last if -e $_;
  524.             }
  525.             
  526.             paraprint <<"EOF" and die "\n" if $sendmail eq "";
  527.             
  528. I am terribly sorry, but I cannot find sendmail, or a close equivalent, and
  529. the perl package Mail::Send has not been installed, so I can't send your bug
  530. report. We apologize for the inconveniencence.
  531.  
  532. So you may attempt to find some way of sending your message, it has
  533. been left in the file `$filename'.
  534.  
  535. EOF
  536.             
  537.             open(SENDMAIL,"|$sendmail -t");
  538.             print SENDMAIL "To: $address\n";
  539.             print SENDMAIL "Subject: $subject\n";
  540.             print SENDMAIL "Cc: $cc\n" if $cc;
  541.             print SENDMAIL "Reply-To: $from\n" if $from;
  542.             print SENDMAIL "\n\n";
  543.             open(REP,"<$filename");
  544.             while(<REP>) { print SENDMAIL $_ }
  545.             close(REP);
  546.             
  547.             close(SENDMAIL);
  548.         }
  549.     
  550.     }
  551.     
  552.     print "\nMessage sent.\n";
  553.  
  554.     1 while unlink($filename);  # remove all versions under VMS
  555.  
  556. }
  557.  
  558. sub Help {
  559.     print <<EOF; 
  560.  
  561. A program to help generate bug reports about perl5, and mail them. 
  562. It is designed to be used interactively. Normally no arguments will
  563. be needed.
  564.     
  565. Usage:
  566. $0  [-v] [-a address] [-s subject] [-b body | -f file ]
  567.     [-r returnaddress] [-e editor] [-c adminaddress | -C] [-S] [-t]
  568.     
  569. Simplest usage:  run "$0", and follow the prompts.
  570.  
  571. Options:
  572.  
  573.   -v    Include Verbose configuration data in the report
  574.   -f    File containing the body of the report. Use this to 
  575.         quickly send a prepared message.
  576.   -S    Send without asking for confirmation.
  577.   -a    Address to send the report to. Defaults to `$address'.
  578.   -c    Address to send copy of report to. Defaults to `$cc'.
  579.   -C    Don't send copy to administrator.
  580.   -s    Subject to include with the message. You will be prompted 
  581.         if you don't supply one on the command line.
  582.   -b    Body of the report. If not included on the command line, or
  583.         in a file with -f, you will get a chance to edit the message.
  584.   -r    Your return address. The program will ask you to confirm
  585.         this if you don't give it here.
  586.   -e    Editor to use. 
  587.   -t    Test mode. The target address defaults to `$testaddress'.
  588.   -d    Data mode (the default if you redirect or pipe output.) 
  589.         This prints out your configuration data, without mailing
  590.         anything. You can use this with -v to get more complete data.
  591.   
  592. EOF
  593. }
  594.  
  595. sub paraprint {
  596.     my @paragraphs = split /\n{2,}/, "@_";
  597.     print "\n\n";
  598.     for (@paragraphs) {   # implicit local $_
  599.         s/(\S)\s*\n/$1 /g;
  600.         write;
  601.         print "\n";
  602.     }
  603.                        
  604. }
  605.                             
  606.  
  607. format STDOUT =
  608. ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~~
  609. $_
  610. .
  611.